package es.mllacer.qqsm.dialogs;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import es.mllacer.qqsm.Play;
import es.mllacer.qqsm.R;

public class NextQuestionDialog extends DialogFragment {

	public static DialogFragment getInstance() {
		return new NextQuestionDialog();
	}

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		setCancelable(false);

		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		builder.setMessage(R.string.play_next_dialog_question);
		builder.setPositiveButton(R.string.play_next_dialog_positive,
				new OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						Play activity = (Play) getActivity();
						// Store scores in local database and remote server
						activity.storeLocalAndRemoteScores();
						// reset preferences
						activity.resetAllSharedPreferences();
						// finish match
						activity.finish();
					}
				});
		builder.setNegativeButton(R.string.play_next_dialog_negative,
				new OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						Play activity = (Play) getActivity();
						// Update counter
						activity.updateCounter(activity.getCounter() + 1);
						// Store counter (question number) preferences
						activity.storeQuestionPreferences();
						// load next question
						activity.loadQuestion();
					}
				});
		return builder.create();
	}
}
